// import Vue from 'vue'
// import VueRouter from 'vue-router'

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        meta: {
            title: '首页'
        },
        component: () => import("@/views/container/Container"),
        children: [
            {
                path: '',
                redirect: '/home/chart'
            },
            {
                path: 'chart',
                meta: {
                    title: '可视化'
                },
                component: () => import("@/views/container/views/chart/Chart")
            },
            {
                path: 'bill',
                meta: {
                    title: '账单数据'
                },
                component: () => import("@/views/container/views/bill/Bill")
            }
        ]
    },
    {
        path: '/login',
        meta: {
            title: '请先登录'
        },
        component: () => import("@/views/login/Login")
    }
]

// eslint-disable-next-line no-undef
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path !== '/login') {
        let token = window.localStorage.getItem("token");
        if (!token) {
            next('/login')
            return ;
        }
    }
    let title = to.meta.title;
    if (title) {
        document.title = title;
    }

    next();
})

export default router
